package server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class Connection extends Thread {

	private DataInputStream in;
	private DataOutputStream out;
	private Socket clientSocket;

	public static final int ADD = 1;
	public static final int SUBTRACT = 2;
	public static final int MULTIPLY = 3;
	public static final int DIVIDE = 4;

	public Connection(Socket clientSocket) {
		try {
			this.clientSocket=clientSocket;
			in = new DataInputStream(clientSocket.getInputStream());
			out = new DataOutputStream(clientSocket.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		try {
			int operation = in.readInt();

			int a = in.readInt();
			int b = in.readInt();

			int result=-1;
			switch (operation) {
			case ADD:
				result = Logic.add(a, b);
				break;
			case SUBTRACT:
				result = Logic.subtract(a, b);
				break;
			case MULTIPLY:
				result = Logic.multiply(a, b);
				break;
			case DIVIDE:
				result = Logic.divide(a, b);
				break;
			}
			
			out.writeInt(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				clientSocket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
